#include "sdk.h"
#include "autodetectcompilers.h"
#include <wx/tooltip.h>
#include "infowindow.h"

BEGIN_EVENT_TABLE( AutoDetectCompilers, wxScrollingDialog )
  EVT_UPDATE_UI( -1, AutoDetectCompilers::OnUpdateUI )
  EVT_BUTTON( XRCID( "btnDefault" ), AutoDetectCompilers::OnDefaultClick )
END_EVENT_TABLE()

AutoDetectCompilers::AutoDetectCompilers( wxWindow* parent ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/auto_detect_compilers.xrc" );
  xml_res->Load( resPath );
  if( !xml_res->LoadObject( this, parent, _T( "dlgAutoDetectCompilers" ), _T( "wxScrollingDialog" ) ) ) {
    return;
  }
  XRCCTRL( *this, "wxID_OK", wxButton )->SetDefault();
  wxListCtrl* list = XRCCTRL( *this, "lcCompilers", wxListCtrl );
  if( list ) {
    list->Connect( wxEVT_MOTION, wxMouseEventHandler( AutoDetectCompilers::OnMouseMotion ) );
    list->ClearAll();
    list->InsertColumn( 0, _T( "Compiler" ), wxLIST_FORMAT_LEFT, 380 );
    list->InsertColumn( 1, _T( "Status" ),   wxLIST_FORMAT_LEFT, 100 );
    for( size_t i = 0; i < CompilerFactory::GetCompilersCount(); ++i ) {
      Compiler* compiler = CompilerFactory::GetCompiler( i );
      if( !compiler ) {
        continue;
      }
      list->InsertItem( list->GetItemCount(), compiler->GetName() );
      wxString path = compiler->GetMasterPath();
      wxString path_no_macros = compiler->GetMasterPath();
      Manager::Get()->GetMacrosManager()->ReplaceMacros( path_no_macros );
      int idx = list->GetItemCount() - 1;
      int highlight = 0;
      if( path.IsEmpty() && Manager::Get()->GetConfigManager( _T( "compiler" ) )->Exists( _T( "/sets/" ) + compiler->GetID() + _T( "/name" ) ) ) {
        list->SetItem( idx, 1, _T( "Invalid" ) );
        highlight = 1;
      } else {
        list->SetItem( idx, 1, _T( "Not found" ) );
        highlight = -1;
      }
      if( compiler->GetParentID().IsEmpty() ) {
        bool detected = compiler->AutoDetectInstallationDir() == adrDetected;
        wxString pathDetected( compiler->GetMasterPath() );
        if( detected ) {
          if( path.IsEmpty() || path == pathDetected || path_no_macros == pathDetected ) {
            list->SetItem( idx, 1, _T( "Detected" ) );
          } else {
            list->SetItem( idx, 1, _T( "User-defined" ) );
          }
          highlight = 0;
        } else if( !path.IsEmpty() ) {
          if( wxFileName::DirExists( path_no_macros ) && !( path == pathDetected || path_no_macros == pathDetected ) ) {
            list->SetItem( idx, 1, _T( "User-defined" ) );
            highlight = 0;
          }
          compiler->SetMasterPath( path );
        }
      } else {
        if( !path.IsEmpty() && wxFileName::DirExists( path_no_macros ) ) {
          list->SetItem( idx, 1, _T( "User-defined" ) );
          highlight = 0;
        }
      }
      if( highlight == 1 ) {
        list->SetItemBackgroundColour( idx, *wxRED );
      } else if( highlight == -1 ) {
        list->SetItemTextColour( idx, *wxLIGHT_GREY );
      }
    }
    list->SetColumnWidth( 0, wxLIST_AUTOSIZE );
    list->SetColumnWidth( 1, wxLIST_AUTOSIZE );
  }
  XRCCTRL( *this, "lblDefCompiler", wxStaticText )->SetLabel( CompilerFactory::GetDefaultCompiler()->GetName() );
}

AutoDetectCompilers::~AutoDetectCompilers() {
}

void AutoDetectCompilers::OnDefaultClick( M_Code_unused wxCommandEvent& event ) {
  wxListCtrl* list = XRCCTRL( *this, "lcCompilers", wxListCtrl );
  int idx = list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
  if( idx != -1 ) {
    CompilerFactory::SetDefaultCompiler( idx );
    XRCCTRL( *this, "lblDefCompiler", wxStaticText )->SetLabel( CompilerFactory::GetDefaultCompiler()->GetName() );
  }
}

void AutoDetectCompilers::OnMouseMotion( wxMouseEvent& event ) {
  wxListCtrl* list = XRCCTRL( *this, "lcCompilers", wxListCtrl );
  int flags = 0;
  int idx = list->HitTest( event.GetPosition(), flags );
  wxString txt = wxEmptyString;
  if( idx != wxNOT_FOUND ) {
    wxListItem itm;
    itm.m_itemId = idx;
    itm.m_col = 1;
    itm.m_mask = wxLIST_MASK_TEXT;
    if( list->GetItem( itm ) ) {
      txt = itm.m_text;
    }
  }
  if( txt == _T( "Detected" ) || txt == _T( "User-defined" ) ) {
    txt = CompilerFactory::GetCompiler( idx )->GetMasterPath();
  } else {
    txt = wxEmptyString;
  }
  if( list->GetToolTip() ) {
    if( txt.IsEmpty() ) {
      list->UnsetToolTip();
    } else if( txt != list->GetToolTip()->GetTip() ) {
      list->SetToolTip( txt );
    }
  } else if( !txt.IsEmpty() ) {
    list->SetToolTip( txt );
  }
}

void AutoDetectCompilers::OnUpdateUI( wxUpdateUIEvent& event ) {
  wxListCtrl* list = XRCCTRL( *this, "lcCompilers", wxListCtrl );
  bool en = list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ) != -1;
  XRCCTRL( *this, "btnDefault", wxButton )->Enable( en );
  event.Skip();
}
